package com.example.lab1.controller.controller;


import com.example.lab1.controller.controller.BaseController;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    private BasicDataSource basicDataSource;

    public UserController(BasicDataSource basicDataSource) {
        this.basicDataSource = basicDataSource;
    }
//    @Autowired//自动装配
//    public void setBasicDataSource(BasicDataSource basicDataSource){
//        this.basicDataSource = basicDataSource;
//    }

    @RequestMapping("signIn")
    private String signIn(@RequestParam String username, @RequestParam String password) throws SQLException {//处理登录请求,username,password 对应表单里面的username，password

        Connection connection = basicDataSource.getConnection();
        String sql = "select * from db_user.user where username = ? and password = ?";

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                request.getSession().setAttribute("username", username);
                return "redirect:/home.jsp";
            } else {
                request.getSession().setAttribute("error", "用户名或密码错误");
                return "redirect:/index.jsp";
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    @RequestMapping("signUP")
    private String signUp(@RequestParam String username, @RequestParam String password) throws SQLException {//处理注册请求

        Connection connection = basicDataSource.getConnection();
        String sql = "insert into db_user.user(username,password) values(?,?)";

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;


        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            preparedStatement.executeUpdate();

            preparedStatement = connection.prepareStatement("select last_insert_id()");
            resultSet = preparedStatement.executeQuery();
            resultSet.next();
            int id = resultSet.getInt(1);

            request.getSession().setAttribute("message", "注册成功你的ID是：" + id);

            return "redirect:/index.jsp";

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("signOut")
    private String signOut() {
        session.invalidate();
        return "redirect:/index.jsp";
    }

}